package cn.itcast.nems.common.web.controller.b;

import cn.itcast.ic.common.bean.BizError;
import cn.itcast.ic.common.bean.Page;
import cn.itcast.ic.common.exception.BizException;
import cn.itcast.nems.base.api.PaymentTypeApi;
import cn.itcast.nems.base.dto.PaymentTypeDTO;
import cn.itcast.nems.base.service.PaymentTypeService;
import cn.itcast.nems.common.PageQueryModel;
import cn.itcast.nems.common.util.NumberUtil;
import cn.itcast.nems.common.web.vo.BasePageRequest;
import cn.itcast.nems.common.web.vo.DictionaryQueryVO;
import cn.itcast.nems.common.web.vo.PaymentTypeEditVO;
import cn.itcast.nems.common.web.vo.PaymentTypeVO;
import cn.itcast.nems.manager.icc.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 付款方式管理
 * </p>
 *
 * @author zhouwei
 * @since 2023/8/30 6:25 PM
 **/
@RestController
@RequestMapping("/b/payment_type")
@Slf4j
public class PaymentTypeController {
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private PaymentTypeApi paymentTypeApi;
    /**
     * 付款类型分页查询
     *
     * @param queryParam 请求参数
     * @return ResponseEntity<?>
     **/
    @PostMapping("/page/list")
    public  ResponseEntity<?> pageList(@RequestBody BasePageRequest<DictionaryQueryVO<?>> queryParam)  {
        queryParam.convert();
        Page<PaymentTypeVO> pageData = Page.of(queryParam.getPageNumber(), queryParam.getPageSize(), 0, null);
        PaymentTypeDTO paymentTypeDTO = new PaymentTypeDTO();
        if (Objects.nonNull(queryParam.getCondition())) {
            if (!NumberUtil.checkNum(Objects.isNull(queryParam.getCondition().getId() )?null:String.valueOf(queryParam.getCondition().getId()),null)) {
                log.info("[付款类型分页查询] 请求参数 编号: {}, 包含非数字内容", queryParam.getCondition().getId());
                return ResponseEntity.ok().body(pageData );
            }
            BeanUtils.copyProperties(queryParam.getCondition(), paymentTypeDTO);
            paymentTypeDTO.setId(Objects.isNull(queryParam.getCondition().getId())?null: NumberUtil.format(String.valueOf(queryParam.getCondition().getId()),10));
        }
        log.info("[付款类型分页查询] 请求参数: {} ", queryParam);
        PageQueryModel<PaymentTypeDTO> queryModel = new PageQueryModel<>(queryParam.getPageNumber(),
                queryParam.getPageSize(), paymentTypeDTO);
        Page<PaymentTypeDTO> data = paymentTypeApi.findPageList(queryModel);
        List<PaymentTypeVO> list = data.getRecords().stream().map(item -> {
                    PaymentTypeVO paymentTypeVO = new PaymentTypeVO();
                    BeanUtils.copyProperties(item, paymentTypeVO);
                    // paymentTypeVO.setId(item.getId());
                    return paymentTypeVO;
                }
        ).toList();
        pageData = Page.of(data.getPageNumber(), data.getPageSize(), data.getRecordCount(), list);
        return ResponseEntity.ok().body(pageData);
    }

    /**
     * 新增付款类型
     *
     * @param editVO 参数对象
     * @return 是否成功
     **/
    @PostMapping()
    public final ResponseEntity<?> addPaymentType(@RequestBody PaymentTypeEditVO editVO) throws Exception {
        if (Objects.isNull(editVO) || editVO.getName().isEmpty()) {
            throw new BizException(BizError.PARAMETER_ERROR);
        }
        if (editVO.getName().length() > CommonConstant.DICTIONARY_NAME_LENGTH) {
            throw new BizException(BizError.PARAMETER_ERROR);
        }
        log.info("[新增业务线] 请求参数: {}", editVO);
        PaymentTypeDTO typeDTO = new PaymentTypeDTO(editVO.getId(), editVO.getCode(), null, editVO.getName(),
                editVO.getState());String result = paymentTypeService.addPaymentType(typeDTO);
        return ResponseEntity.ok().body(result);
    }

    /**
     * 修改付款类型
     *
     * @param editVO 参数对象
     * @return 是否成功
     **/
    @PutMapping()
    public final ResponseEntity<?> updatePaymentType(@RequestBody PaymentTypeEditVO editVO) {
        if (Objects.isNull(editVO) || editVO.getName().isEmpty() || Objects.isNull(editVO.getId())) {
            throw  new BizException(BizError.PARAMETER_ERROR);
        }
        if(editVO.getName().length() > CommonConstant.DICTIONARY_NAME_LENGTH) {
            throw  new BizException(BizError.PARAMETER_ERROR);
        }
        log.info("[新增业务线] 请求参数: {}", editVO);
        PaymentTypeDTO typeDTO = new PaymentTypeDTO(editVO.getId(), null, null, editVO.getName(),
                editVO.getState());
        String   result = paymentTypeService.updatePaymentType(typeDTO);
        return ResponseEntity.ok().body(result);
    }

    /**
     * 启用/禁用付款类型
     *
     * @param editVO 参数对象
     * @return 是否成功
     **/
    @PutMapping("/state")
    public final ResponseEntity<?> updateState(@RequestBody PaymentTypeEditVO editVO) {
        if (Objects.isNull(editVO) || Objects.isNull(editVO.getState()) || Objects.isNull(editVO.getId())) {
            throw  new BizException(BizError.PARAMETER_ERROR);
        }
        log.info("[新增付款类型] 请求参数: {}", editVO);
        PaymentTypeDTO typeDTO = new PaymentTypeDTO(editVO.getId(), null, null, editVO.getName(),
                editVO.getState());
        String result = paymentTypeService.updateState(typeDTO);
        return ResponseEntity.ok().body(result);
    }
}
